tinyMCEPopup.requireLangPack();

var StyleLibraryHeadingDialog = {
	init : function() {
		var f = document.forms[0];

		document.getElementById('previewbg_pickcontainer').innerHTML = getColorPickerHTML('previewbg_pick','previewbg');

		fntFont.init(tinyMCEPopup.editor);
		cellProp.init(tinyMCEPopup.editor);
		BorderPicker.init(tinyMCEPopup.editor);
		mrgMargin.init(tinyMCEPopup.editor);
		
		TinyMCE_EditableSelects.init();
		
		initDesigner("dialog");
	}
};

tinyMCEPopup.onInit.add(StyleLibraryHeadingDialog.init, StyleLibraryHeadingDialog);

function initDesigner(caller)
{
	// Run only if all loaded
	switch(caller) {
		case "frame":
			frameLoaded = true;
			break;
		case "dialog":
			dialogLoaded = true;
			break;
		case "css":
			if(tinymce.isGecko) { return; }  // Never runs. Gecko does not call this way.
			cssLoaded = true;
			break;
	}
	if(!frameLoaded || !dialogLoaded || (!tinymce.isGecko && !cssLoaded)) {
		return;
	}

	// Configure which rules allow what properties.
	for(var i = 0; i < designer.rules.length; i++)
	{
		// All fonts use only font rules.
		designer.rules[i].properties.initFont();
		designer.rules[i].properties.initBorder();
		designer.rules[i].properties.initCellProp();
		designer.rules[i].properties.killProperty('vertical-align');
		designer.rules[i].properties.initProperty('margin-top');
		designer.rules[i].properties.initProperty('margin-bottom');
	}

	// Load necessary CSS files	in preview frame
	loadPreviewStyles(window.frames['demoFrame'].document, 
		tinyMCEPopup.editor.getParam("stylelibrary_previewstyles", 
			tinyMCEPopup.editor.getParam("content_css")));

	// Parse input, get dom pointers, create dom rules.
	designer.init(window.frames['demoFrame'].document, "wysiwyg");
	
	previewSettings.switchLorem(false, true);
	previewSettings.demoBgInit();
	setTask('style');
}

function updateDemo()
{
	var output = document.getElementById("hdnCssOutput");
	
	designer.generateOutput();
	designer.updateCssDom();
	output.value = removeNL(designer.getCss());
	
	if(DEBUGJS)
	{
		alert(output.value);
	}
}

// ****** EVENTS ********

function taskChanged()
{
	var hx = getStyle(); // Which heading is edited?
	var task = getTask();

	switch(task) {
		case "style":
			BorderPicker.initFromRule(designer.rules[hx]);
			fntFont.initFromRule(designer.rules[hx]);
			cellProp.initFromRule(designer.rules[hx]);
			mrgMargin.initFromRule(designer.rules[hx]);
			break;
	}
}
